<?php

/**
 * This is the model class for table "review".
 *
 * The followings are the available columns in table 'review':
 * @property integer $id
 * @property integer $uid
 * @property integer $place_id
 * @property string $text
 * @property string $time
 * @property integer $all_like
 * @property integer $all_dislike
 * @property integer $rate
 */
class Review extends ActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Review the static model class
	 */
	public $otherAttributes = array();
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'review';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('uid, place_id, text', 'required'),
			array('uid, place_id, all_like, all_dislike, rate', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, uid, place_id, text, time, all_like, all_dislike, rate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'account'=>array(self::BELONGS_TO,'Account','uid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'uid' => 'Uid',
			'place_id' => 'Place',
			'text' => 'Text',
			'time' => 'Time',
			'rate' => 'Rate',
			'all_like' => 'All Like',
			'all_dislike' => 'All Dislike',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('place_id',$this->place_id);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('rate',$this->rate);
		$criteria->compare('all_like',$this->all_like);
		$criteria->compare('all_dislike',$this->all_dislike);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getIterator()
	{
		$attributes = $this->getAttributes();
		$relations = array();
		                
		foreach ($this->relations() as $key => $related)
		{
			if ($this->hasRelated($key))
			{
				$relations[$key] = $this->$key;
			}
		}
		
		$all = array_merge($attributes, $relations, $this->otherAttributes);
		return new CMapIterator($all);
	}
	
	public function afterFind()
	{
		if(Yii::app()->user->id == $this->uid){
			$this->otherAttributes['owner'] = false;
		}
		else{
			$this->otherAttributes['owner'] = true;
		}
		if(ReviewFeedback::model()->findByAttributes(array('uid'=>Yii::app()->user->id, 'review_id'=>$this->id, 'type'=>1)) == null){
			$this->otherAttributes['isLike'] = false;
		}
		else{
			$this->otherAttributes['isLike'] = true;
		}
		if(ReviewFeedback::model()->findByAttributes(array('uid'=>Yii::app()->user->id, 'review_id'=>$this->id, 'type'=>0)) == null){
			$this->otherAttributes['isDislike'] = false;
		}
		else{
			$this->otherAttributes['isDislike'] = true;
		}
	}
}